<template>
  <div class="user-list-container">
    <h2 class="page-title">用户管理</h2>
    
    <el-card shadow="never" class="toolbar-card">
      <el-row :gutter="20">
        <el-col :span="18">
          <el-space wrap>
            <el-select v-model="filters.gender" placeholder="性别" clearable style="width: 100px" @change="loadList">
              <el-option label="全部" :value="undefined" />
              <el-option label="男" :value="1" />
              <el-option label="女" :value="2" />
            </el-select>
            
            <el-select v-model="filters.vipLevel" placeholder="VIP等级" clearable style="width: 120px" @change="loadList">
              <el-option label="全部" :value="undefined" />
              <el-option label="普通用户" :value="0" />
              <el-option label="VIP会员" :value="1" />
            </el-select>
            
            <el-input v-model="filters.keyword" placeholder="搜索昵称/手机" clearable style="width: 200px" @keyup.enter="loadList">
              <template #append>
                <el-button icon="Search" @click="loadList" />
              </template>
            </el-input>
            
            <el-button icon="Refresh" @click="loadList">刷新</el-button>
          </el-space>
        </el-col>
      </el-row>
    </el-card>
    
    <el-card shadow="never" class="table-card">
      <el-table v-loading="loading" :data="list" stripe>
        <el-table-column type="index" label="序号" width="60" />
        <el-table-column prop="userId" label="用户ID" width="80" />
        <el-table-column prop="avatarUrl" label="头像" width="80">
          <template #default="{ row }">
            <el-avatar :src="row.avatarUrl" />
          </template>
        </el-table-column>
        <el-table-column prop="nickname" label="昵称" width="120" />
        <el-table-column prop="phone" label="手机号" width="130" />
        <el-table-column prop="gender" label="性别" width="60">
          <template #default="{ row }">
            <el-tag :type="row.gender === 1 ? 'primary' : 'danger'" size="small">
              {{ row.gender === 1 ? '男' : '女' }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="age" label="年龄" width="60" />
        <el-table-column prop="city" label="城市" width="100" />
        <el-table-column prop="vipLevel" label="VIP等级" width="100">
          <template #default="{ row }">
            <el-tag :type="row.vipLevel > 0 ? 'warning' : ''" size="small">
              {{ row.vipLevel > 0 ? 'VIP会员' : '普通用户' }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="registrationTime" label="注册时间" width="180" />
        <el-table-column prop="status" label="状态" width="80">
          <template #default="{ row }">
            <el-tag :type="row.status === 1 ? 'success' : 'danger'" size="small">
              {{ row.status === 1 ? '正常' : '禁用' }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="200" fixed="right">
          <template #default="{ row }">
            <el-button type="primary" size="small" link @click="handleViewDetail(row)">详情</el-button>
            <el-button :type="row.status === 1 ? 'danger' : 'success'" size="small" link @click="handleToggleStatus(row)">
              {{ row.status === 1 ? '禁用' : '启用' }}
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      
      <div class="pagination-container">
        <el-pagination
          v-model:current-page="currentPage"
          v-model:page-size="pageSize"
          :total="total"
          layout="total, sizes, prev, pager, next"
          @size-change="loadList"
          @current-change="loadList"
        />
      </div>
    </el-card>
  </div>
</template>

<script setup>
import { ref, reactive, onMounted } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import request from '@/utils/request'
import { API_ENDPOINTS } from '@/config/api'

const loading = ref(false)
const currentPage = ref(1)
const pageSize = ref(10)
const total = ref(0)
const list = ref([])

const filters = reactive({
  gender: null,
  vipLevel: null,
  keyword: ''
})

const loadList = async () => {
  loading.value = true
  try {
    const response = await request.get(API_ENDPOINTS.USER_LIST, {
      params: { page: currentPage.value, pageSize: pageSize.value, ...filters }
    })
    if (response.code === 200) {
      list.value = response.data.list || response.data || []
      total.value = response.data.total || list.value.length
    }
  } catch (error) {
    console.error('加载失败:', error)
    ElMessage.error('加载失败')
  } finally {
    loading.value = false
  }
}

const handleViewDetail = (row) => {
  ElMessage.info('用户详情功能开发中')
  // TODO: 实现用户详情查看
}

const handleToggleStatus = async (row) => {
  try {
    const action = row.status === 1 ? '禁用' : '启用'
    await ElMessageBox.confirm(`确定要${action}该用户吗？`, '提示', { type: 'warning' })
    
    const newStatus = row.status === 1 ? 0 : 1
    const response = await request.put(`${API_ENDPOINTS.USER_UPDATE}/${row.userId}`, { status: newStatus })
    
    if (response.code === 200) {
      ElMessage.success(`${action}成功`)
      row.status = newStatus
    }
  } catch (error) {
    if (error !== 'cancel') console.error('操作失败:', error)
  }
}

onMounted(() => loadList())
</script>

<style scoped>
.user-list-container { padding: 0; }
.page-title { font-size: 24px; font-weight: bold; color: #333; margin: 0 0 20px 0; }
.toolbar-card { margin-bottom: 20px; }
.table-card { margin-top: 20px; }
.pagination-container { display: flex; justify-content: flex-end; margin-top: 20px; }
</style>

